package com.gxan.web.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 字符过滤类
 */
public class CharsetFilter implements Filter {

    private String charset;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 获取初始参数
        charset = filterConfig.getInitParameter("code");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException, IOException {
        // 请求
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        // 响应
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //设置字符编码
        request.setCharacterEncoding(charset);
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }

}
